using System;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Input;
using System.Collections.Generic;

namespace Nyanimals
{
	public sealed class CInput
	{
		private static readonly CInput instance = new CInput();
		private GamePadData gamePadData;
		private TouchStatus touchStatus;
		private Vector2 touchPoint;
        private List<TouchData> TouchD;
		public static readonly float LEFT_DEADZONE = 0.15f;
		public static readonly float RIGHT_DEADZONE = 0.15f;
		
		private bool touchDown;
		
		private CInput ()
		{
			touchDown = false;
		}
        
        public List<TouchData> TouchDataList
        {
            get { return TouchD;}
        }
		
		static public CInput Instance
		{
			get { return instance;}
		}
		
		public void Update()
		{
			gamePadData = GamePad.GetData(0);
			
			// NOTE: this will only read the first touch point
			TouchD = Touch.GetData(0);
			if(TouchD.Count > 0)
			{
				TouchData d = TouchD[0];
				
				/*if(touchStatus != d.Status)
				{
					Console.WriteLine("Touch: " + d.Status.ToString());
				}*/
				touchStatus = d.Status;
				touchPoint = new Vector2(d.X, d.Y);
				if(d.Status == TouchStatus.Down)
				{
					touchDown = true;
				}
				else if(d.Status == TouchStatus.Up)
				{
					touchDown = false;
				}		
			}
		}
		
		// touch data
		public TouchStatus Status
		{
			get { return touchStatus;}
		}
		public  bool Touching
		{
			get { return touchDown;}
		}
		
		public Vector2 TouchPoint
		{
			get { return touchPoint;}
		}
		
		// button inputs
		// NOTE: Look into how these are all handled(check if this would be correct...)
		public bool ButtonPressed(GamePadButtons button)
		{
			return (gamePadData.ButtonsDown & button) != 0;
		}
		public bool ButtonReleased(GamePadButtons button)
		{
			return (gamePadData.ButtonsUp & button) != 0;
		}
		public bool ButtonHeld(GamePadButtons button)
		{
			return (gamePadData.Buttons & button) != 0;
		}
		
		// analog stick data
		public Vector2 LeftAnalog
		{
			get { return new Vector2(gamePadData.AnalogLeftX, gamePadData.AnalogLeftY);}
		}
		public Vector2 RightAnalog
		{
			get { return new Vector2(gamePadData.AnalogRightX, gamePadData.AnalogRightY);}
		}
		public float GetLeftAnalogX()
		{
			return gamePadData.AnalogLeftX;
		}
		public float GetLeftAnalogY()
		{
			return gamePadData.AnalogLeftY;
		}
		public float GetRightAnalogX()
		{
			return gamePadData.AnalogRightX;
		}
		public float GetRightAnalogY()
		{
			return gamePadData.AnalogRightY;
		}
	}
}

